package com.study.dynamic.controller;

import com.study.dynamic.context.CustomApplicationContext;
import com.study.dynamic.service.UserService;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariProxyConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Objects;

/**
 * @author Mayjean
 * @Date2020/9/9
 */
@RestController
@RequestMapping("/test")
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    private CustomApplicationContext customApplicationContext;

    @GetMapping(value = "/load1" )
    public String load1(){
        System.out.println(userService.getByUserId("1"));
        getBean();
        registerBean1();
        getBean();
        System.out.println(userService.getByUserId("1"));
        return "SUCCESS 1:"+new Date();
    }

    @GetMapping(value = "/load2" )
    public String load2(){
        registerBean2();
        getBean();
        System.out.println(userService.getByUserId("1"));
        return "SUCCESS 2:"+new Date();
    }


    private void getBean() {
        //HikariProxyConnection@1742608098 wrapping com.mysql.jdbc.JDBC4Connection@25a18cb2
        DataSource dataSource = customApplicationContext.getBean(DataSource.class);
        try {
            System.out.println(dataSource.getConnection());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void registerBean1(){
        //HikariProxyConnection connection = new HikariProxyConnection();
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setConnectionTestQuery("select 1");
        dataSource.setConnectionTimeout(60000);
        dataSource.setMinimumIdle(2);
        dataSource.setMaximumPoolSize(100);
        dataSource.setMaxLifetime(600000);
        dataSource.setValidationTimeout(5000);
        dataSource.setIdleTimeout(300000);
        dataSource.setLeakDetectionThreshold(500000);

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://192.168.1.15:3306/mall_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        testConn(dataSource);

        //customApplicationContext.registerBean("dataSource",dataSource.getClass(),null);
        customApplicationContext.registerSingleton("dataSource",dataSource);
    }

    public void registerBean2(){
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setConnectionTestQuery("select 1");
        dataSource.setConnectionTimeout(60000);
        dataSource.setMinimumIdle(2);
        dataSource.setMaximumPoolSize(100);
        dataSource.setMaxLifetime(600000);
        dataSource.setValidationTimeout(5000);
        dataSource.setIdleTimeout(300000);
        dataSource.setLeakDetectionThreshold(500000);

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://192.168.1.15:3306/seata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        testConn(dataSource);
        customApplicationContext.registerSingleton("dataSource",dataSource);
    }

    private void testConn(DataSource dataSource){
        try{
            Connection connection  = dataSource.getConnection();
            String sql = "select * from ti_user where use_id='1' limit 1";
            PreparedStatement pst = connection.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            if(Objects.nonNull(rs)){
                while(rs.next()){
                    System.out.println(rs.getInt("id")+"-"+rs.getString("username"));
                }
            }
            rs.close();
            pst.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}